import getpass, imaplib
from properties import Properties

class SkypeSortEmail():		
	def sortMails(obj):
		p = Properties()
		p.load(open('skype.properties'))
		
		host = p['email4syncHost']
		print host
		port = p['email4syncPort']
		print port
		
		M = imaplib.IMAP4_SSL(host,int(port))
		M.login(p['email4syncUsername'], p['email4syncPassword'])
		print M.list()
		M.select('[Gmail]/All Mail')
		typ, data = M.search(None, 'ALL')
		
		for num in data[0].split():			
			typ, data = M.fetch(num, '(RFC822.SIZE BODY[HEADER.FIELDS (FROM)])')			
			print type(data)
			dialogPartners = data[0][1].lstrip('From: " ').strip().rstrip('"@'+p['smtpserver']).strip()			
			M.create(dialogPartners)
			M.copy(num,dialogPartners)
			M.store(num, '+FLAGS', '\\Deleted')			
		
		M.expunge()
		M.close()
		M.logout()

sorting=SkypeSortEmail()

sorting.sortMails()